package com.agy.filemanager.service.impl;

import com.agy.filemanager.common.StringUtils;
import com.agy.filemanager.common.TreeVo;
import com.agy.filemanager.entity.DcDocProjectStep;
import com.agy.filemanager.entity.FileType;
import com.agy.filemanager.entity.FileTypeExample;
import com.agy.filemanager.entity.TreeModel;
import com.agy.filemanager.mapper.FileTypeMapper;
import com.agy.filemanager.mapper.ProStageMapper;
import com.agy.filemanager.service.ProStageService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * 项目阶段
 */
@Service
public class ProStageServiceImpl implements ProStageService{

    @Autowired
    private ProStageMapper proStageMapper;

    @Autowired
    private FileTypeMapper fileTypeMapper;
    /**
     * 项目大类和项目分类树
     * @return
     */
    @Override
    public List<TreeModel> selectTree(){
        return this.proStageMapper.selectTree();
    }

    /**
     * 查询项目阶段表
     * @param pageSize
     * @param pageNum
     * @param id
     * @return
     */
    @Override
    public Page selectTable(int pageSize, int pageNum, String id,String name) {
        Page page = PageHelper.startPage(pageNum,pageSize);
        List<DcDocProjectStep> list = null;
        Example example = new Example(DcDocProjectStep.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("isyes","1");
        if(id != null && !id.equals("")) {
            criteria.andEqualTo("projectType", id);
        }
        if(name != null && !name.equals("")) {
            criteria.andLike("name", "%"+name+"%");
        }
        example.setOrderByClause("sort desc");
        list = this.proStageMapper.selectByExample(example);
        return page;
    }

    /**
     * 保存项目阶段
     * @param dcDocProjectStep
     */
    @Override
    public void insertStage(DcDocProjectStep dcDocProjectStep) {
        try{
            if((dcDocProjectStep.getId()).equals("")){
                dcDocProjectStep.setId(StringUtils.getUUID());
                dcDocProjectStep.setIsyes(new Byte("1"));
                int sort = this.proStageMapper.selectCount(null) + 1;
                dcDocProjectStep.setSort(sort);
                this.proStageMapper.insertEntity(dcDocProjectStep);
            }else{
                this.proStageMapper.updateEntityById(dcDocProjectStep);
            }
        }
        catch (Exception ex){
            ex.printStackTrace();
        }

    }

    /**
     * 删除项目阶段
     * @param id
     */
    @Override
    public void deleteStage(String id) {
        DcDocProjectStep entity = new DcDocProjectStep();
        entity.setId(id);
        this.proStageMapper.deleteByPrimaryKey(entity);
    }

    /**
     * 查询项目阶段列表
     * @param proTypeId x项目分类id
     * @return
     */
    @Override
    public List<TreeVo> getTreeVoByProTypeId(String proTypeId) {
        List<DcDocProjectStep> projectSteps = proStageMapper.getProStepByProTypeId(proTypeId);
        List<TreeVo> treeVos = new ArrayList<>();
        for (DcDocProjectStep proStep:
             projectSteps) {
            TreeVo treeVo = new TreeVo();
            treeVo.setId(proStep.getId());
            treeVo.setText(proStep.getName());
            //文档文件夹查询
            FileTypeExample example = new FileTypeExample();
            example.setOrderByClause("SORT ASC");
            FileTypeExample.Criteria criteria = example.createCriteria();
            List<FileType> fileTypes = fileTypeMapper.selectByExample(example);
            if(fileTypes == null||fileTypes.size()==0){
                continue;
            }
            //初始化数据
            treeVo.initList(fileTypes.size());
            for (FileType fileType:
                  fileTypes) {
                TreeVo vo = new TreeVo();
                vo.setId(fileType.getId());
                vo.setText(fileType.getName());
                treeVo.getNodes().add(vo);
            }
            treeVos.add(treeVo);
        }
        return treeVos;
    }
}
